<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var longestCommonPrefix = function(strs) {
      const noEmpty = strs.every((item)=> item != '' )
      if(!noEmpty) return ''
      let prefix = ''
      const firstStrArr = []
      for(let i=0; i< strs.length; i++) {
          const first = strs[i].slice(0,1)
          if(!first) return ''
          firstStrArr.push(first)
          strs[i] = strs[i].slice(1)
      }
      if(!firstStrArr.length) return ''
      const isSame = firstStrArr.every((item)=> {
          return item === firstStrArr[0]
      })
      prefix = isSame ? firstStrArr[0] : ''
      
      return prefix ? prefix + longestCommonPrefix(strs) : prefix
      
    };


// var longestCommonPrefix = function(strs) {
//     let res = ''
//     let idx = 1
//     let flag = true
//     while(flag) {
//         const temp = strs[0].slice(0, idx)
        
//         for(let i=0; i< strs.length; i++) {
//             const item = strs[i]
//             if(item.length <= idx) return res
//             if(temp !== item.slice(0, idx)) {
//                 flag = false
//             }
//         }
//         if(flag) {
//             res = temp
//             idx++
//         }
        
//     }
//     return res
    
// };
    console.log(longestCommonPrefix(["a"]))
  </script>
</body>
</html>